<?php  

namespace App\Services;
use Log;

class HtmlsafeService
{
    public static function htmlSafeCheck($links_id,$html) {
        // remove all non-printable characters. CR(0a) and LF(0b) and TAB(9) are allowed
        // this prevents some character re-spacing such as <java\0script>
        // note that you have to handle splits with \n, \r, and \t later since they *are* allowed in some inputs
        $html = preg_replace('/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/', '', $html);
        // straight replacements, the user should never need these since they're normal characters
        // this prevents like <IMG SRC=@avascript:alert('XSS')>
        $search = 'abcdefghijklmnopqrstuvwxyz';
        $search .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $search .= '1234567890!@#$%^&*()';
        $search .= '~`";:?+/={}[]-_|\'\\';
        for ($i = 0; $i < strlen($search); $i++) {
            // ;? matches the ;, which is optional
            // 0{0,7} matches any padded zeros, which are optional and go up to 8 chars
            // @ @ search for the hex values
            $html = preg_replace('/([xX]0{0,8}'.dechex(ord($search[$i])).';?)/i', $search[$i], $html); // with a ;
            // @ @ 0{0,7} matches '0' zero to seven times
            $html = preg_replace('/(�{0,8}'.ord($search[$i]).';?)/', $search[$i], $html); // with a ;
        }

        $ttwlLinks = array('pdb2.com', '11dr.cn', 'sds99a.cn', 'aixin7.cn','b6s.cn');
        $hasttwl = false;
        foreach ($ttwlLinks as $ttwlLink) {
            if (strpos($html, $ttwlLink) !== false) {
                $hasttwl = true;
            }
        }
        if ($hasttwl) return ["status"=>"error","msg"=>"请不要输入天天外链链接,请删除链接后选择跳转小程序!"];

        // now the only remaining whitespace attacks are \t, \n, and \r
        $mysqlarrays = array('select', 'insert', 'and', 'update', 'delete', 'union', 'into', 'load_file', 'outfile');
        $hasxss = false;

        foreach ($mysqlarrays as $mysqlarray) {
            if (strpos($html, $mysqlarray) !== false) {
                $hasxss = true;
            }
        }
        if ($hasxss) {
            Log::info("编辑器SQL注入-----------");
            Log::info($links_id);
            Log::info($html);
            Log::info("编辑器SQL注入-----------");
            return ["status"=>"error","msg"=>"您输入的内容存在安全风险,系统已记录您的请求数据与信息;再次提交将会封禁账号!"];
        }
        $xssList1 = array('javascript', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink','<style', 'script', 'embed', 'object', 'iframe', 'frame', 'frameset', 'ilayer', 'layer', 'bgsound', 'title', 'base');
        $xssLists2 = array('onabort', 'onactivate', 'onafterprint', 'onafterupdate', 'onbeforeactivate', 'onbeforecopy', 'onbeforecut', 'onbeforedeactivate', 'onbeforeeditfocus', 'onbeforepaste', 'onbeforeprint', 'onbeforeunload', 'onbeforeupdate', 'onblur', 'onbounce', 'oncellchange', 'oncontextmenu', 'oncontrolselect', 'oncopy', 'oncut', 'ondataavailable', 'ondatasetchanged', 'ondatasetcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart', 'ondrop', 'onerror', 'onerrorupdate', 'onfilterchange', 'onfinish', 'onfocus', 'onfocusin', 'onfocusout', 'onhelp', 'onkeydown', 'onkeypress', 'onkeyup', 'onlayoutcomplete', 'onload', 'onlosecapture', 'onmousedown', 'onmouseenter', 'onmouseleave', 'onmousemove', 'onmouseout', 'onmouseover', 'onmouseup', 'onmousewheel', 'onmove', 'onmoveend', 'onmovestart', 'onpaste', 'onpropertychange', 'onreadystatechange', 'onreset', 'onresize', 'onresizeend', 'onresizestart', 'onrowenter', 'onrowexit', 'onrowsdelete', 'onrowsinserted', 'onscroll', 'onselect', 'onselectionchange', 'onselectstart', 'onstart', 'onstop', 'onsubmit', 'onunload','onclick','onchange');
        $xssLists = array_merge($xssList1, $xssLists2);

        foreach ($xssLists as $xssList) {
            if (strpos($html, $xssList) !== false) {
                $hasxss = true;
            }
        }
        if ($hasxss) {
            Log::info("编辑器XSS注入-----------");
            Log::info($links_id);
            Log::info($html);
            Log::info("编辑器XSS注入-----------");
            return ["status"=>"error","msg"=>"您输入的内容存在安全风险,系统已记录您的请求数据与信息;再次提交将会封禁账号!"];
        }
        return ["status"=>"success"];
        
    }
}